1.3 随机数组random
1.3.1 随机产生1个小数
import numpy as np
t=np.random.rand() #默认产生1个随机数
print(t)
返回:
0.03924480867779023
1.3.2 随机产生指定个数的小数
import numpy as np
t=np.random.rand( 3 ) #产生3个随机数
print(t)
返回:[0.35183594 0.27558799 0.85495117]
1.3.3 随机产生指定个数和维数的小数
import numpy as np
t=np.random.rand( 2,3 ) #产生2维数组,前面第1+个参数是维数,最后的参数是元素个数
print (t)
返回:
[[0.68223417 0.05312258 0.44102174]
[0.84928039 0.68856544 0.55637787]]
1.3.4 随机产生小数(前期绑定函数)
from numpy.random import rand #只导入numpy 里面的rand函数
print (rand(2))
返回:
[0.72030193 0.91877214]
1.3.5 随机产生指定范围区间的整数
import numpy as py
t=np.random.randint( 1,5 ) #随机产生1-5之间的整数
print (t)
返回:
2
1.3.6 随机产生整数并设置维数(元组形式)
import numpy as np
t=np.random.randint(1,5, size =( 2,10 )) #随机产生1个2维数组 ,用()是元组形式
print (t)
返回:
[[3 1 1 3 3 1 3 3 3 4]
[4 1 2 3 2 1 1 2 1 2]]
1.3.7 随机产生整数并设置维数(数组形式)
import numpy as np
t=np.random.randint( 1,5, size =[ 2,10 ]) #随机产生1个2维数组 ,用[ ]是数组形式,结果一样
print (t)
返回:
[[3 1 1 3 3 1 3 3 3 4]
[4 1 2 3 2 1 1 2 1 2]]